package net.homeip.mmhg.game;

import org.joda.time.DateTime;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Gives next hint to the team.
 * 
 * @author pilot
 */
@Component
public class ChangeTaskStateJob implements StatefulJob {

	public static String POSITION_ID = "positionId";
	private static int STD_INTERVAL = 30;
	
	private GameContext gameContext;
	
	public static Trigger getStdTrigger() {
		Trigger trigger = TriggerUtils.makeMinutelyTrigger(STD_INTERVAL);
		trigger.setStartTime(new DateTime().plusMinutes(STD_INTERVAL).toDate());
		return trigger;
	}
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			JobDataMap dataMap = context.getMergedJobDataMap();
			gameContext.changePositionState((Integer) dataMap.get(POSITION_ID));
		} catch (Throwable throwable) {
			throw new JobExecutionException(throwable);
		}
	}

	@Autowired
	public void setGameContext(GameContext context) {
		this.gameContext = context;
	}
}
